package java44th.group.service.wx.address;

import java44th.group.bean.po.MarketAddress;
import java44th.group.bean.po.MarketAddressExample;
import java44th.group.bean.po.MarketCoupon;
import java44th.group.bean.wx.vo.address.WxAddressListVo;
import java44th.group.bean.wx.vo.address.WxAddressVo;
import java44th.group.mapper.MarketAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

/**
 * @Aurhor: 王可龙
 * @DATE: 2022/10/14 23:28
 * @Package:java44th.group.service.wx.address
 * @Description:
 * @Version 1.0
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;

    /**
     * 改代码，根据用户的id 去进行查找地址，进行显示
     * @return
     */
    @Override
    public WxAddressListVo list(Integer userId) {
        MarketAddress marketAddress1 = new MarketAddress();
        marketAddress1.setUserId(userId);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectAddressByUserId(marketAddress1);
        long count = marketAddressMapper.countByExample(null);
        WxAddressListVo wxAddressListVo = new WxAddressListVo();
        LinkedList<WxAddressVo> wxAddressVos = new LinkedList<>();
        if (marketAddresses==null){
            return wxAddressListVo;
        }
        for (MarketAddress marketAddress : marketAddresses) {
            WxAddressVo wxAddressVo = new WxAddressVo();
            SetWxAddressVo(marketAddress, wxAddressVo);
            wxAddressVos.add(wxAddressVo);
        }

        wxAddressListVo.setTotal((int) count);
        wxAddressListVo.setPages(1);
        wxAddressListVo.setLimit((int) count);
        wxAddressListVo.setPage(1);
        wxAddressListVo.setList(wxAddressVos);
        return wxAddressListVo;
    }

    /**
     * list清单中的，数据库中的时间信息转化成可以响应的字符串信息
     * @param marketAddress
     * @param wxAddressVo
     */
    private void SetWxAddressVo(MarketAddress marketAddress, WxAddressVo wxAddressVo) {
        wxAddressVo.setId(marketAddress.getId());
        wxAddressVo.setName(marketAddress.getName());
        wxAddressVo.setUserId(marketAddress.getUserId());
        wxAddressVo.setProvince(marketAddress.getProvince());
        wxAddressVo.setCity(marketAddress.getCity());
        wxAddressVo.setCounty(marketAddress.getCounty());
        wxAddressVo.setAddressDetail(marketAddress.getAddressDetail());
        wxAddressVo.setAreaCode(marketAddress.getAreaCode());
        wxAddressVo.setTel(marketAddress.getTel());
        wxAddressVo.setIsDefault(marketAddress.getIsDefault());
        wxAddressVo.setAddTime(marketAddress.getAddTime().toString());
        wxAddressVo.setUpdateTime(marketAddress.getUpdateTime().toString());
        wxAddressVo.setDeleted(marketAddress.getDeleted());
    }

    @Override
    public WxAddressVo detail(Integer id) {
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);
        WxAddressVo wxAddressVo = new WxAddressVo();
        if (marketAddress==null){
            return wxAddressVo;
        }
        SetWxAddressVo(marketAddress, wxAddressVo);
        return wxAddressVo;
    }

    @Override
    public Integer save(MarketAddress marketAddress,Integer userId) {
        //更新
        if (marketAddress.getId()!=null) {
            MarketAddress marketAddress1 = marketAddressMapper.selectByPrimaryKey(marketAddress.getId());
            if (marketAddress1!=null){
                marketAddress1.setUserId(userId);
                marketAddress1.setAddressDetail(marketAddress.getAddressDetail());
                marketAddress1.setAreaCode(marketAddress.getAreaCode());
                marketAddress1.setCity(marketAddress.getCity());
                marketAddress1.setCounty(marketAddress.getCounty());
                if (marketAddress.getIsDefault()){
                    MarketAddress marketAddress2=marketAddressMapper.selectIsDefault();
                    if (marketAddress1!=null){
                        //将数据库中是默认的地址标记为false
                        marketAddress2.setIsDefault(false);
                        marketAddressMapper.updateByPrimaryKey(marketAddress2);
                    }
                }
                marketAddress1.setIsDefault(marketAddress.getIsDefault());
                marketAddress1.setName(marketAddress.getName());
                marketAddress1.setProvince(marketAddress.getProvince());
                marketAddress1.setTel(marketAddress.getTel());
                marketAddress1.setUpdateTime(new Timestamp(System.currentTimeMillis()));
                int i = marketAddressMapper.updateByPrimaryKeySelective(marketAddress1);
                return marketAddress1.getId();
            }
        }
        //新增
        if (marketAddress.getIsDefault()){
            MarketAddress marketAddress1=marketAddressMapper.selectIsDefault();
            if (marketAddress1!=null){
                //将数据库中是默认的地址标记为false
                marketAddress1.setIsDefault(false);
                marketAddressMapper.updateByPrimaryKey(marketAddress1);
            }
        }
        marketAddress.setAddTime(new Timestamp(System.currentTimeMillis()));
        marketAddress.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        marketAddress.setDeleted(false);
        marketAddress.setUserId(userId);
        marketAddressMapper.insertSelective(marketAddress);
        return marketAddress.getId();

    }

    @Override
    public Integer delete(MarketAddress marketAddress) {

        int i = marketAddressMapper.deleteByPrimaryKey(marketAddress.getId());
        return i;
    }


}
